package com.smasher.mqtt.config;

import com.smasher.mqtt.message.LastWillMessage;

public class EasyMqttOptions {

    /**
     * 服务器地址（协议+地址+端口号）
     */
    private String address;
    /**
     * 用户名
     */
    private String userName;
    /**
     * 密码
     */
    private String passWord;

    /**
     * 客户端标识
     */
    private String clientId;

    /**
     * 最大重连间隔
     */
    private int maxReconnectDelay;

    /**
     * 心跳包发送间隔，单位：秒
     */
    private int keepAliveInterval;

    /**
     * 连接超时时间，单位：秒
     */
    private int connectTimeout;

    /**
     * 自动重连
     */
    private boolean automaticReconnect;

    /**
     * 默认订阅主题
     */
    private String defaultTopic;

    /**
     * lastWillMessage
     */
    private LastWillMessage defaultTopicWillMessage;

    /**
     * 默认订阅主题Qos
     */
    private int defaultTopicQos;

    /**
     * 默认订阅主题是否保留
     */
    private boolean defaultTopicRetained;


    /**
     * 构建者
     */
    public static class Builder {

        EasyMqttOptions options;

        public Builder() {
            this(getDefaultOptions());
        }

        public Builder(EasyMqttOptions defaultOptions) {
            options = defaultOptions;
        }

        public Builder setHost(String host) {
            options.address = host;
            return this;
        }

        public Builder setUserName(String userName) {
            options.userName = userName;
            return this;
        }

        public Builder setPassWord(String passWord) {
            options.passWord = passWord;
            return this;
        }

        public Builder setClientId(String clientId) {
            options.clientId = clientId;
            return this;
        }

        public Builder setMaxReconnectDelay(int maxReconnectDelay) {
            options.maxReconnectDelay = maxReconnectDelay;
            return this;
        }

        public Builder setKeepAliveInterval(int keepAliveInterval) {
            options.keepAliveInterval = keepAliveInterval;
            return this;
        }

        public Builder setConnectTimeout(int connectTimeout) {
            options.connectTimeout = connectTimeout;
            return this;
        }

        public Builder setAutomaticReconnect(boolean automaticReconnect) {
            options.automaticReconnect = automaticReconnect;
            return this;
        }

        public Builder setDefaultTopic(String defaultTopic) {
            options.defaultTopic = defaultTopic;
            return this;
        }

        public Builder setDefaultTopicWillMessage(LastWillMessage defaultTopicWillMessage) {
            options.defaultTopicWillMessage = defaultTopicWillMessage;
            return this;
        }

        public Builder setDefaultTopicQos(int defaultTopicQos) {
            options.defaultTopicQos = defaultTopicQos;
            return this;
        }

        public Builder setDefaultTopicRetained(boolean defaultTopicRetained) {
            options.defaultTopicRetained = defaultTopicRetained;
            return this;
        }

        public EasyMqttOptions build() {
            return options;
        }
    }

    public static EasyMqttOptions getDefaultOptions() {
        EasyMqttOptions options = new EasyMqttOptions();
        options.address = "tcp://192.168.1.1:1883";
        options.userName = "userName";
        options.passWord = "passWord";
        options.clientId = "clientId";
        options.maxReconnectDelay = 1000;
        options.keepAliveInterval = 60;
        options.connectTimeout = 10;
        options.automaticReconnect = true;
        options.defaultTopic = null;
        options.defaultTopicWillMessage = null;
        options.defaultTopicQos = 1;
        options.defaultTopicRetained = true;
        return options;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public int getMaxReconnectDelay() {
        return maxReconnectDelay;
    }

    public void setMaxReconnectDelay(int maxReconnectDelay) {
        this.maxReconnectDelay = maxReconnectDelay;
    }

    public int getKeepAliveInterval() {
        return keepAliveInterval;
    }

    public void setKeepAliveInterval(int keepAliveInterval) {
        this.keepAliveInterval = keepAliveInterval;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public boolean isAutomaticReconnect() {
        return automaticReconnect;
    }

    public void setAutomaticReconnect(boolean automaticReconnect) {
        this.automaticReconnect = automaticReconnect;
    }

    public String getDefaultTopic() {
        return defaultTopic;
    }

    public void setDefaultTopic(String defaultTopic) {
        this.defaultTopic = defaultTopic;
    }

    public LastWillMessage getDefaultTopicWillMessage() {
        return defaultTopicWillMessage;
    }

    public void setDefaultTopicWillMessage(LastWillMessage defaultTopicWillMessage) {
        this.defaultTopicWillMessage = defaultTopicWillMessage;
    }

    public int getDefaultTopicQos() {
        return defaultTopicQos;
    }

    public void setDefaultTopicQos(int defaultTopicQos) {
        this.defaultTopicQos = defaultTopicQos;
    }

    public boolean isDefaultTopicRetained() {
        return defaultTopicRetained;
    }

    public void setDefaultTopicRetained(boolean defaultTopicRetained) {
        this.defaultTopicRetained = defaultTopicRetained;
    }
}
